package com.ftg.learn.util;

import com.ftg.learn.comm.RMessCommEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * @author 布衣
 */
@Data
@AllArgsConstructor
@Schema(name = "封装对象")
public class R<T> {
    @Schema(description = "状态码")
    private Integer code;
    @Schema(description = "信息")
    private String msg;
    @Schema(description = "数据")
    private T data;

    public static <T> R<T> ok(T data) {
        return new R<>(RMessCommEnum.R_MESS_SUCCESS_ENUM.getCode(), RMessCommEnum.R_MESS_SUCCESS_ENUM.getMsg(), data);
    }

    public static <T> R<T> failure() {
        return new R<>(RMessCommEnum.R_MESS_FAILURE_ENUM.getCode(), RMessCommEnum.R_MESS_FAILURE_ENUM.getMsg(), null);
    }

    public static <T> R<T> build(RMessCommEnum r) {
        return new R<>(r.getCode(), r.getMsg(), null);
    }

    public static <T> R<T> build(RMessCommEnum r, T data) {
        return new R<>(r.getCode(), r.getMsg(), data);
    }
}
